package com.mfangsoft.zhuangjialong.common.controller;

import java.net.URLEncoder;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.mfangsoft.zhuangjialong.common.model.ResponseMessage;
import com.mfangsoft.zhuangjialong.common.utils.SysConstant;
/**
 * 文件下载
 * @author top
 *
 */
@Controller
@RequestMapping("/common")
public class FileDownloadController {
	/**
	 * 文件下载
	 */
	@RequestMapping(value = "/download", method = RequestMethod.GET)
	@ResponseBody
	@CrossOrigin
	public ResponseMessage<Object> upload(HttpServletRequest request, HttpServletResponse response) {
		ResponseMessage<Object> message = new ResponseMessage<Object>();
		try {
			String name = request.getParameter("name");
			System.out.println(name);
			//将name设置为iso编码
			String ISO_name = new String(name.getBytes("utf-8"),"ISO-8859-1");
			//设置头通知，告诉浏览器以附件下载的方式打开 
			response.setHeader("Content-Disposition", "attachment;filename="+ISO_name);
			request.getRequestDispatcher("/download/"+name).forward(request, response);
		} catch (Exception e) {
			message.setCode(SysConstant.FAILURE_CODE);
			message.setMessage(SysConstant.FAILURE_MSG);
			e.printStackTrace();
		}
		return message;
	}
}
